package com.cloud.form.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.cloud.form.api.bean.Assembly;
import com.cloud.form.api.entity.FormAssemblyDef;
import com.cloud.form.api.entity.FormAssemblyInst;
import com.cloud.form.api.entity.FormFieldAuth;
import com.cloud.form.api.mapper.FormAssemblyDefMapper;
import com.cloud.form.api.mapper.FormAssemblyInstMapper;
import com.cloud.form.api.service.FormAuthorityService;
import com.cloud.form.api.service.FormDragerService;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class FormDragerServiceImpl implements FormDragerService {

	@Autowired
	private FormAssemblyDefMapper formAssemblyDefMapper;

	@Autowired
	private FormAssemblyInstMapper formAssemblyInstMapper;
	
	@Autowired
	private FormAuthorityService formAuthorityService;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<FormAssemblyDef> listAssemblies() {
		Example example = new Example(FormAssemblyDef.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("enableFlag", 1);
		return formAssemblyDefMapper.selectByExample(example);
	}

	@Override
	public long createAssemby(Assembly assembly) {
		FormAssemblyInst inst = new FormAssemblyInst();
		inst.setFormId(assembly.getFormId());
		inst.setControlType(assembly.getControlType());
		inst.setAssemblyName(assembly.getName());
		inst.setAssemblyValue(assembly.getValue());
		inst.setAuthFlag(assembly.getAuthFlag());
		inst.setPositionX(assembly.getAttributes().getX());
		inst.setPositionY(assembly.getAttributes().getY());
		if (null != assembly.getAttributes()) {
			inst.setStyleAttributes(JSON.toJSONString(assembly.getAttributes()));
		}
		if (null != assembly.getValidators()) {
			inst.setValidAttributes(JSON.toJSONString(assembly.getValidators()));
		}
		if(null != assembly.getConfigs()){
			inst.setConfigAttributes(JSON.toJSONString(assembly.getConfigs()));
		}
		formAssemblyInstMapper.insertSelective(inst);
		return inst.getId();
	}

	@Override
	public List<FormAssemblyInst> loadFormAssemblies(Long formId, Long actDefId) {
		Example example = new Example(FormAssemblyInst.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("formId", formId);
		example.orderBy("displayOrder").asc();
		List<FormAssemblyInst> assemblies = formAssemblyInstMapper.selectByExample(example);
		if(null != actDefId && actDefId > 0){
			//编辑模式，需要考虑权限问题
			List<FormFieldAuth> fieldAuths = formAuthorityService.loadFormActFieldAuth(formId, actDefId);
			for(FormAssemblyInst assembly : assemblies){
				for(FormFieldAuth fieldAuth : fieldAuths){
					if(assembly.getId().equals(fieldAuth.getFieldId())){
						assembly.setAuthCode(fieldAuth.getAuthCode());
					}
				}
			}
		}
		return assemblies;
	}

	@Override
	public void move(long id, int x, int y) {
		jdbcTemplate.update("update form_assembly_inst set position_x = ?, position_y = ? where id = ?", x, y, id);
	}

	@Override
	public void saveAssemblies(List<Assembly> assemblies) {
		for (Assembly assembly : assemblies) {
			FormAssemblyInst inst = new FormAssemblyInst();
			inst.setId(assembly.getId());
			inst.setAssemblyName(assembly.getName());
			inst.setAssemblyLabelName(assembly.getLabelName());
			inst.setAssemblyValue(assembly.getValue());
			inst.setAuthFlag(assembly.getAuthFlag());
			inst.setStyleAttributes(JSON.toJSONString(assembly.getAttributes()));
			inst.setPositionX(assembly.getAttributes().getX());
			inst.setPositionY(assembly.getAttributes().getY());
			if (null != assembly.getValidators()) {
				inst.setValidAttributes(JSON.toJSONString(assembly.getValidators()));
			}
			if (null != assembly.getConfigs()) {
				inst.setConfigAttributes(JSON.toJSONString(assembly.getConfigs()));
			}
			formAssemblyInstMapper.updateByPrimaryKeySelective(inst);
		}
	}

	@Override
	public void delete(long id) {
		this.formAssemblyInstMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void clear(long formId) {
		Example example = new Example(FormAssemblyInst.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("formId", formId);
		this.formAssemblyInstMapper.deleteByExample(example);
	}

}
